package com.xzzz.irda.tracker.core.adapter.spring;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author wangyf
 * @since 1.2.0
 */
@Slf4j
@Configuration
public class TrackerSpringConfiguration {

    @Bean
    public TrackerFilter trackerFilter () {
        return new TrackerFilter();
    }

//    @Bean("taskExecutor")
//    public TaskExecutor taskExecutor() {
//        log.info("[TRACKERS] 已适配 SPRING_TASK");
//        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
//        // 核心线程池10
//        executor.setCorePoolSize(10);
//        // 最大线程池20
//        executor.setMaxPoolSize(20);
//        // 队列容量
//        executor.setQueueCapacity(200);
//        // 当线程空闲时间达到keepAliveTime，该线程会退出，直到线程数量等于corePoolSize。
//        executor.setKeepAliveSeconds(60);
//        // 线程名称前缀
//        executor.setThreadNamePrefix("xzzz-task-");
//        // 用来设置线程池关闭的时候等待所有任务都完成再继续销毁其他的Bean
//        executor.setWaitForTasksToCompleteOnShutdown(true);
//        // 等待时间
//        executor.setAwaitTerminationSeconds(60);
//        // 增加 TaskDecorator 属性的配置
//        executor.setTaskDecorator(new TraceTaskDecorator("SPRING_ASYNC_TASK", TraceConstants.SPAN_TYPE_SPRING_ASYNC));
//        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
//        // 处理未捕获异常日志打印
//        final Thread.UncaughtExceptionHandler uncaughtExceptionHandler = (t, e) -> log.error("异步线程执行失败。异常信息 => {} : ", e.getMessage(), e);
//        ThreadFactoryBuilder threadFactoryBuilder = new ThreadFactoryBuilder();
//        threadFactoryBuilder.setNamePrefix("xzzz-task-");
//        threadFactoryBuilder.setUncaughtExceptionHandler(uncaughtExceptionHandler);
//        executor.setThreadFactory(threadFactoryBuilder.build());
//        executor.initialize();
//        return executor;
//    }
}
